home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrscript.win / feature files / vrhash.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.7 KB  |  281 lines

  1. //////////
  2. //
  3. //    File:        VRHash.c
  4. //
  5. //    Contains:    Functions for hash table management.
  6. //
  7. //    Written by:    Tim Monroe
  8. //
  9. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <3>         12/02/98        rtm        simplified VRHash_DestroyHashTable
  14. //       <2>         11/29/98        rtm        minor clean-up to some routines
  15. //       <1>         11/28/98        rtm        first file (Happy Thanksgiving!)
  16. //    
  17. //    Our hash table is an array of "bucket pointers"; each bucket (defined by the VRScriptHash
  18. //    data structure) contains the command word, its corresponding command code, and a pointer to
  19. //    the next bucket in the linked list (or NULL if there is no next bucket).
  20. //
  21. //    There are many ways to implement a hash table management scheme; the one used here is eerily
  22. //    similar (as I now discover) to the one described by Kernighan & Ritchie in The C Programming
  23. //    Language.
  24. //
  25. //////////
  26.  
  27. #include "VRHash.h"
  28.  
  29. // global variables
  30.  
  31. static VRScriptHashPtr            gHashTable[kNumEntriesInTable];            // the hash table
  32.  
  33.  
  34. //////////
  35. //
  36. // VRHash_CreateHashTable
  37. // Create the hash table.
  38. //
  39. //////////
  40.  
  41. void VRHash_CreateHashTable (void)
  42. {
  43.     short            myIndex;
  44.         
  45.     // initialize the bucket pointers in the hash table
  46.     for (myIndex = 0; myIndex < kNumEntriesInTable; myIndex++)
  47.         gHashTable[myIndex] = NULL;
  48.     
  49.     // insert entries into the hash table
  50.     VRHash_PutCommandIntoTable("OpenQTVRMovieFile", kOpenQTVRMovieFile);
  51.     VRHash_PutCommandIntoTable("ReplaceMainMovie", kReplaceMainMovie);
  52.     VRHash_PutCommandIntoTable("SetCurrentDirectory", kSetCurrentDirectory);
  53.     VRHash_PutCommandIntoTable("SetBarState", kSetBarState);
  54.     VRHash_PutCommandIntoTable("SetButtonState", kSetButtonState);
  55.     VRHash_PutCommandIntoTable("SetResizeState", kSetResizeState);
  56.     VRHash_PutCommandIntoTable("SetWindowSize", kSetWindowSize);
  57.     VRHash_PutCommandIntoTable("SetMaxWindowSize", kSetMaxWindowSize);
  58.     VRHash_PutCommandIntoTable("ReplaceCursor", kReplaceCursor);
  59.     VRHash_PutCommandIntoTable("SetHotSpotIDCursors", kSetHotSpotIDCursors);
  60.     VRHash_PutCommandIntoTable("SetHotSpotTypeCursors", kSetHotSpotTypeCursors);
  61.     VRHash_PutCommandIntoTable("GoToNodeID", kGoToNodeID);
  62.     VRHash_PutCommandIntoTable("ShowDefaultView", kShowDefaultView);
  63.     VRHash_PutCommandIntoTable("OpenResourceFile", kOpenResourceFile);
  64.     VRHash_PutCommandIntoTable("SetCorrection", kSetCorrection);
  65.     VRHash_PutCommandIntoTable("SetQuality", kSetQuality);
  66.     VRHash_PutCommandIntoTable("SetSwingSpeed", kSetSwingSpeed);
  67.     VRHash_PutCommandIntoTable("SetSwingDirection", kSetSwingDirection);
  68.     VRHash_PutCommandIntoTable("SetSwingState", kSetSwingState);
  69.     VRHash_PutCommandIntoTable("SetPanAngle", kSetPanAngle);
  70.     VRHash_PutCommandIntoTable("SetTiltAngle", kSetTiltAngle);
  71.     VRHash_PutCommandIntoTable("SetPanTiltZoom", kSetPanTiltZoom);
  72.     VRHash_PutCommandIntoTable("SetFieldOfView", kSetFieldOfView);
  73.     VRHash_PutCommandIntoTable("SetViewCenter", kSetViewCenter);
  74.     VRHash_PutCommandIntoTable("SetPanLimits", kSetPanLimits);
  75.     VRHash_PutCommandIntoTable("SetTiltLimits", kSetTiltLimits);
  76.     VRHash_PutCommandIntoTable("SetZoomLimits", kSetZoomLimits);
  77.     VRHash_PutCommandIntoTable("SetHotSpotState", kSetHotSpotState);
  78.     VRHash_PutCommandIntoTable("SetTranslateState", kSetTranslateState);
  79.     VRHash_PutCommandIntoTable("SetClickRadius", kSetClickRadius);
  80.     VRHash_PutCommandIntoTable("SetClickTimeout", kSetClickTimeout);
  81.     VRHash_PutCommandIntoTable("SetPanTiltSpeed", kSetPanTiltSpeed);
  82.     VRHash_PutCommandIntoTable("SetZoomSpeed", kSetZoomSpeed);
  83.     VRHash_PutCommandIntoTable("SetMouseScale", kSetMouseScale);
  84.     VRHash_PutCommandIntoTable("SetFrameRate", kSetFrameRate);
  85.     VRHash_PutCommandIntoTable("SetViewRate", kSetViewRate);
  86.     VRHash_PutCommandIntoTable("SetViewTime", kSetViewTime);
  87.     VRHash_PutCommandIntoTable("SetViewState", kSetViewState);
  88.     VRHash_PutCommandIntoTable("SetAnimationState", kSetAnimationState);
  89.     VRHash_PutCommandIntoTable("SetControlState", kSetControlState);
  90.     VRHash_PutCommandIntoTable("SetFrameAnimState", kSetFrameAnimState);
  91.     VRHash_PutCommandIntoTable("SetViewAnimState", kSetViewAnimState);
  92.     VRHash_PutCommandIntoTable("SetQTVRVisState", kSetQTVRVisState);
  93.     VRHash_PutCommandIntoTable("SetCachePrefs", kSetCachePrefs);
  94.     VRHash_PutCommandIntoTable("SetMovieVolume", kSetMovieVolume);
  95.     VRHash_PutCommandIntoTable("SetTrackVolume", kSetTrackVolume);
  96.     VRHash_PutCommandIntoTable("SetSoundVolume", kSetSoundVolume);
  97.     VRHash_PutCommandIntoTable("SetSoundBalance", kSetSoundBalance);
  98.     VRHash_PutCommandIntoTable("PlaySceneSound", kPlaySceneSound);
  99.     VRHash_PutCommandIntoTable("PlaySceneQTMidi", kPlaySceneQTMidi);
  100.     VRHash_PutCommandIntoTable("PlayNodeQTMidi", kPlayNodeQTMidi);
  101.     VRHash_PutCommandIntoTable("PlayNodeSound", kPlayNodeSound);
  102.     VRHash_PutCommandIntoTable("PlayNode3DSound", kPlayNode3DSound);
  103.     VRHash_PutCommandIntoTable("HotSpotQTMidi", kHotSpotQTMidi);
  104.     VRHash_PutCommandIntoTable("HotSpotSound", kHotSpotSound);
  105.     VRHash_PutCommandIntoTable("HotSpot3DSound", kHotSpot3DSound);
  106.     VRHash_PutCommandIntoTable("HotSpotMovie", kHotSpotMovie);
  107.     VRHash_PutCommandIntoTable("TriggerHotSpot", kTriggerHotSpot);
  108.     VRHash_PutCommandIntoTable("PlayQTMidi", kPlayQTMidi);
  109.     VRHash_PutCommandIntoTable("PlaySndResource", kPlaySndResource);
  110.     VRHash_PutCommandIntoTable("PlaySound", kPlaySndResource);                // synonym
  111.     VRHash_PutCommandIntoTable("PlaySoundFile", kPlaySoundFile);
  112.     VRHash_PutCommandIntoTable("Play3DSndResource", kPlay3DSndResource);
  113.     VRHash_PutCommandIntoTable("Play3DSndResourceAngle", kPlay3DSndResourceAngle);
  114.     VRHash_PutCommandIntoTable("ShowPicture", kShowPicture);
  115.     VRHash_PutCommandIntoTable("ShowNodePicture", kShowNodePicture);
  116.     VRHash_PutCommandIntoTable("AtTime", kAtTime);
  117.     VRHash_PutCommandIntoTable("AtAppLaunch", kAtAppLaunch);
  118.     VRHash_PutCommandIntoTable("AtAppQuit", kAtAppQuit);
  119.     VRHash_PutCommandIntoTable("AtMouseOverHSID", kAtMouseOverHSID);
  120.     VRHash_PutCommandIntoTable("AtMouseOverHSType", kAtMouseOverHSType);
  121.     VRHash_PutCommandIntoTable("AtClickHSID", kAtClickHSID);
  122.     VRHash_PutCommandIntoTable("AtClickHS", kAtClickHSID);                    // synonym
  123.     VRHash_PutCommandIntoTable("AtClickHSType", kAtClickHSType);
  124.     VRHash_PutCommandIntoTable("AtClickCustomButton", kAtClickCustomButton);
  125.     VRHash_PutCommandIntoTable("AtClickSprite", kAtClickSprite);
  126.     VRHash_PutCommandIntoTable("AtNodeEntry", kAtNodeEntry);
  127.     VRHash_PutCommandIntoTable("AtNodeExit", kAtNodeExit);
  128.     VRHash_PutCommandIntoTable("AtPanAngle", kAtPanAngle);
  129.     VRHash_PutCommandIntoTable("AtTiltAngle", kAtTiltAngle);
  130.     VRHash_PutCommandIntoTable("AtZoomAngle", kAtZoomAngle);
  131.     VRHash_PutCommandIntoTable("DoBoth", kDoBoth);
  132.     VRHash_PutCommandIntoTable("DoNothing", kDoNothing);
  133.     VRHash_PutCommandIntoTable("PlayMovie", kPlayMovie);
  134.     VRHash_PutCommandIntoTable("PlayTransMovie", kPlayTransMovie);
  135.     VRHash_PutCommandIntoTable("PlayTransEffect", kPlayTransEffect);
  136.     VRHash_PutCommandIntoTable("MoveScreen", kMoveScreen);
  137.     VRHash_PutCommandIntoTable("Beep", kBeep);
  138.     VRHash_PutCommandIntoTable("ProcessScript", kProcessScript);
  139.     VRHash_PutCommandIntoTable("CreateBox", kCreateBox);
  140.     VRHash_PutCommandIntoTable("CreateCone", kCreateCone);
  141.     VRHash_PutCommandIntoTable("CreateCylinder", kCreateCylinder);
  142.     VRHash_PutCommandIntoTable("CreateEllipsoid", kCreateEllipsoid);
  143.     VRHash_PutCommandIntoTable("CreateTorus", kCreateTorus);
  144.     VRHash_PutCommandIntoTable("CreateRectangle", kCreateRectangle);
  145.     VRHash_PutCommandIntoTable("Open3DMFFile", kOpen3DMFFile);
  146.     VRHash_PutCommandIntoTable("Set3DObjLocation", kSet3DObjLocation);
  147.     VRHash_PutCommandIntoTable("Set3DObjColor", kSet3DObjColor);
  148.     VRHash_PutCommandIntoTable("Set3DObjTransp", kSet3DObjTransp);
  149.     VRHash_PutCommandIntoTable("Set3DObjInterp", kSet3DObjInterp);
  150.     VRHash_PutCommandIntoTable("Set3DObjBackface", kSet3DObjBackface);
  151.     VRHash_PutCommandIntoTable("Set3DObjFill", kSet3DObjFill);
  152.     VRHash_PutCommandIntoTable("Set3DObjRotation", kSet3DObjRotation);
  153.     VRHash_PutCommandIntoTable("Set3DObjRotState", kSet3DObjRotState);
  154.     VRHash_PutCommandIntoTable("Set3DObjVisState", kSet3DObjVisState);
  155.     VRHash_PutCommandIntoTable("Set3DObjTexture", kSet3DObjTexture);
  156.     VRHash_PutCommandIntoTable("Destroy3DObject", kDestroy3DObject);
  157.     VRHash_PutCommandIntoTable("Set3DSndLocation", kSet3DSndLocation);
  158.     VRHash_PutCommandIntoTable("SetVariable", kSetVariable);
  159.     VRHash_PutCommandIntoTable("If", kIf);
  160.     VRHash_PutCommandIntoTable("SetSpriteVisState", kSetSpriteVisState);
  161.     VRHash_PutCommandIntoTable("SetSpriteLayer", kSetSpriteLayer);
  162.     VRHash_PutCommandIntoTable("SetSpriteGraphicsMode", kSetSpriteGraphicsMode);
  163.     VRHash_PutCommandIntoTable("SetSpriteImageIndex", kSetSpriteImageIndex);
  164.     VRHash_PutCommandIntoTable("SetSpriteMatrix", kSetSpriteMatrix);
  165.     VRHash_PutCommandIntoTable("SetSpriteLocation", kSetSpriteLocation);
  166.     VRHash_PutCommandIntoTable("SetTrackState", kSetTrackState);
  167.     VRHash_PutCommandIntoTable("SetTrackLayer", kSetTrackLayer);
  168.     VRHash_PutCommandIntoTable("SetMovieTime", kSetMovieTime);
  169.     VRHash_PutCommandIntoTable("SetMovieRate", kSetMovieRate);
  170.     VRHash_PutCommandIntoTable("SetMovieTimeScale", kSetMovieTimeScale);
  171. }
  172.  
  173.  
  174. //////////
  175. //
  176. // VRHash_DestroyHashTable
  177. // Destroy the hash table.
  178. //
  179. //////////
  180.  
  181. void VRHash_DestroyHashTable (void)
  182. {
  183.     UInt32                myIndex;
  184.     VRScriptHashPtr        myBucketPtr;
  185.     VRScriptHashPtr        myNextBucketPtr;
  186.  
  187.     for (myIndex = 0; myIndex < kNumEntriesInTable; myIndex++) {
  188.     
  189.         // get the bucket pointer at this offset in the hash table
  190.         myBucketPtr = gHashTable[myIndex];
  191.  
  192.         while (myBucketPtr != NULL) {
  193.             myNextBucketPtr = myBucketPtr->fNextEntry;
  194.             free(myBucketPtr->fCommandWord);
  195.             DisposePtr((Ptr)myBucketPtr);
  196.             myBucketPtr = myNextBucketPtr;
  197.         }
  198.     }
  199. }
  200.  
  201.  
  202. //////////
  203. //
  204. // VRHash_HashCommandWord
  205. // Get the hash value for the specified command word.
  206. //
  207. // This hash function returns a value in the range 0 to kNumEntriesInTable - 1. It's
  208. // a simple additive hash function. (As K&R put it, "[t]his is not the best possible
  209. // algorithm, but it has the merit of extreme simplicity".)
  210. //
  211. //////////
  212.  
  213. UInt32 VRHash_HashCommandWord (char *theCommandWord)
  214. {
  215.     UInt32        myHashVal;
  216.  
  217.     for (myHashVal = 0; *theCommandWord != '\0'; myHashVal += *theCommandWord++)
  218.         ;
  219.  
  220.     return(myHashVal % kNumEntriesInTable);
  221. }
  222.  
  223.  
  224. //////////
  225. //
  226. // VRHash_PutCommandIntoTable
  227. // Insert an entry for the specified command word and command code into the hash table.
  228. //
  229. //////////
  230.  
  231. void VRHash_PutCommandIntoTable (char *theCommandWord, UInt32 theCommandCode)
  232. {
  233.     UInt32                myHashVal;
  234.     VRScriptHashPtr        myBucketPtr;
  235.     
  236.     if (theCommandWord == NULL)
  237.         return;
  238.  
  239.     // get the hash value for the specified command word
  240.     myHashVal = VRHash_HashCommandWord(theCommandWord);
  241.     
  242.     // create a new bucket and insert it at the head of the linked list
  243.     myBucketPtr = (VRScriptHashPtr)NewPtrClear(sizeof(VRScriptHash));
  244.     if (myBucketPtr != NULL) {
  245.         myBucketPtr->fCommandCode = theCommandCode;
  246.         myBucketPtr->fCommandWord = malloc(strlen(theCommandWord) + 1);
  247.         strncpy(myBucketPtr->fCommandWord, theCommandWord, strlen(theCommandWord) + 1);
  248.         myBucketPtr->fNextEntry = gHashTable[myHashVal];
  249.         gHashTable[myHashVal] = myBucketPtr;
  250.     }
  251. }
  252.  
  253.  
  254. //////////
  255. //
  256. // VRHash_GetCommandCode
  257. // Get the command code associated with the specified command word.
  258. //
  259. //////////
  260.  
  261. UInt32 VRHash_GetCommandCode (char *theCommandWord)
  262. {
  263.     UInt32                myCode = kInvalidCommand;
  264.     UInt32                myHashVal;
  265.     VRScriptHashPtr        myBucketPtr;
  266.     
  267.     if (theCommandWord == NULL)
  268.         return(myCode);
  269.  
  270.     // get the hash value for the specified command word
  271.     myHashVal = VRHash_HashCommandWord(theCommandWord);
  272.     
  273.     for (myBucketPtr = gHashTable[myHashVal]; myBucketPtr != NULL; myBucketPtr = myBucketPtr->fNextEntry)
  274.         if (strcmp(theCommandWord, myBucketPtr->fCommandWord) == 0)
  275.             return(myBucketPtr->fCommandCode);
  276.  
  277.     return(myCode);
  278. }
  279.  
  280.  
  281.